<template>
  <div class="login_view">
    <div class="main">
      <div class="login_view_header">
        <img
          src="@/assets/images/m_logo.png"
          class="login_view_header_logo"
        />
      </div>
      <div class="login_view_container">
        <div class="login_view_container_form">
          <h3 class="login_view_container_form_title">登录moodytiger系统</h3>
          <p class="login_view_container_form_desc">点击以下任意系统进入</p>
          <div class="login_view_container_form_item" @click="doLogin('pdm')">moodytiger PDM</div>
          <div class="login_view_container_form_item" @click="doLogin('scm')">moodytiger SCM</div>
        </div>
      </div>
    </div>
  </div>
</template>
<script setup>
import { useRoute } from 'vue-router';
import '@/utils/rem';
import { getLogin, getScmLogin } from '@/api/user';
import { message } from 'ant-design-vue';

const Route = useRoute();

const setCookie = (key, value, expire) => {
  const d = new Date()
  d.setDate(d.getDate() + expire)
  document.cookie = `${key}=${value};path=/;expries=${d.toUTCString()}`
}

const doLogin = (url) => {
  const list = JSON.parse(sessionStorage.getItem('sso'));
  const thirdId = url === 'pdm' ? '6522737a4c7d6516277a3c94' : 'scm';
  const item = list.find(item => item.thirdId === thirdId);
  if (!item) {
    message.error('权限不足');
    return;
  }
  const { realname, password } = item;
  if (url === 'pdm') {
    const params = {
      userName: realname,
      passWord: password,
      company: thirdId
    };
    getLogin(params).then(res => {
      let {
          message
        } = res;
      window.location.href = message;
    });
  } else {
    const params = {
      userName: realname,
      passWord: password,
      company: 1
    };
    getScmLogin(params).then(res => {
      let { sessionKey } = res;
      // setCookie('sessionKey', sessionKey, 24 * 60 * 60 * 1000);
      window.location.href = `http://202.105.104.158:8202/srm/main.html?key=${sessionKey}`;
      // window.location.href = `http://202.105.104.158:8189/srm/main.html?key=${sessionKey}`;
    });
  }
}

</script>
<style lang="less" scoped>
@import "./index.less";
</style>
